import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class CheckConfig {

	private static List<ImageSize> androidIcons = new ArrayList<ImageSize>();
	private static List<ImageSize> androidSlidesAndStarts = new ArrayList<ImageSize>();
	private static List<ImageSize> iosIcons = new ArrayList<ImageSize>();
	private static List<ImageSize> iosSlidesAndStarts = new ArrayList<ImageSize>();

	private static final int[][] icons = { { 48, 48 }, { 72, 72 }, { 96, 96 }, { 144, 144 } };
	private static final int[][] slides = { { 480, 800 }, { 540, 960 }, { 720, 1280 }, { 1080, 1920 }
//	, { 1080, 2160 }
	};

	private static final int[][] ios_icons = { { 29, 29 }, { 40, 40 }, { 48, 48 }, { 57, 57 }, { 58, 58 }, { 60, 60 },
			{ 72, 72 }, { 80, 80 }, { 87, 87 }, { 96, 96 }, { 114, 114 }, { 120, 120 }, { 144, 144 }, { 171, 171 },
			{ 180, 180 }, { 1024, 1024 } };

	private static final int[][] ios_slides = { { 480, 800 }, { 540, 960 }, { 640, 1136 }, { 640, 960 }, { 720, 1280 },
			{ 750, 1334 }, { 1080, 1920 }, { 1242, 2208 } };

	private static final String CONFIG = "config_android.txt";

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			System.out.println("请输入检测文件夹的路径");
			return;
		}
		initValues();
		for (String arg : args) {
			checkAll(arg);
		}
	}

	private static void initValues() {
		androidIcons = getDefaultSize(icons);
		androidSlidesAndStarts = getDefaultSize(slides);
		iosIcons = getDefaultSize(ios_icons);
		iosSlidesAndStarts = getDefaultSize(ios_slides);
	}

	public static boolean check(String path) {
		initValues();
		return checkAll(path);
	}

	private static boolean checkAll(String path) {
		boolean isResult = true;

		File f = new File(path);
		if (!f.exists()) {
			System.out.println("错误:文件夹路径不存在!请输入正确的路径!");
			return false;
		}
		if (!f.isDirectory()) {
			System.out.println("错误:请输入要检测的文件夹路径!");
			return false;
		}
		String[] dirs = f.list();
		dirs = Utils.removeHiddleDir(dirs);

		for (String dir : dirs) {
			String dirPath = path + PackApk.PATH_SEPARATE + dir;
			if (!checkAndroidPic(dirPath) || !checkAndroidConfig(dirPath)) {
				isResult = false;
			}
		}
		return isResult;
	}

	private static boolean checkAndroidPic(String basePath) {
		writeToResult("正在检测" + getName(basePath) + "的Android资源文件");
		if (checkPic(basePath, "icon", "Android") && checkPic(basePath, "slide", "Android")
				&& checkPic(basePath, "start", "Android")) {
			writeToResult(getName(basePath) + "的Android资源图片检测通过!\n");
			return true;
		} else {
			writeToResult("错误:" + getName(basePath) + "的Android资源图片有问题，请检查!\n");
			return false;
		}
	}

	@SuppressWarnings("unused")
	private static void checkIosPic(String basePath) {
		writeToResult("正在检测" + getName(basePath) + "的IOS资源文件");
		if (checkPic(basePath, "icon", "ios") && checkPic(basePath, "slide", "ios")
				&& checkPic(basePath, "start", "ios")) {
			writeToResult(getName(basePath) + "的IOS资源图片检测通过!\n");
		} else {
			writeToResult("错误:" + getName(basePath) + "的IOS资源图片有问题，请检查!\n");
		}
	}

	private static String getName(String basePath) {
		String[] str;
		if (PackApk.IS_WINDOWS) {
			str = basePath.split("\\\\");
		} else {
			str = basePath.split("/");
		}
		return str[str.length - 1];
	}

	private static boolean checkPic(String basePath, String iconType, String osType) {
		String path;
		boolean isSuccess = true;
		switch (iconType) {
			case "icon":

				path = basePath + PackApk.PATH_SEPARATE + "pic" + PackApk.PATH_SEPARATE + "icon";
				writeToResult("正在检测" + getName(basePath) + "的icon图片文件夹...");

				if (osType.equals("Android")) {
					return containsList(getImagesSizeList(path), androidIcons);
				} else {
					return containsList(getImagesSizeList(path), iosIcons);
				}
			case "slide":
				writeToResult("正在检测" + getName(basePath) + "的slide图片文件夹...");
				for (int i = 1; i <= 3; i++) {
					writeToResult("\t正在检测" + getName(basePath) + "slide文件夹" + i);
					path = basePath + PackApk.PATH_SEPARATE + "pic" + PackApk.PATH_SEPARATE + "slide"
							+ PackApk.PATH_SEPARATE + i;

					if (osType.equals("Android")) {
						if (!containsList(getImagesSizeList(path), androidSlidesAndStarts)) {
							isSuccess = false;
						}
					} else {
						if (!containsList(getImagesSizeList(path), iosSlidesAndStarts)) {
							isSuccess = false;
						}
					}
				}
				break;
			case "start":
				writeToResult("正在检测" + getName(basePath) + "的start图片文件夹...");
				path = basePath + PackApk.PATH_SEPARATE + "pic" + PackApk.PATH_SEPARATE + "start";

				if (osType.equals("Android")) {
					return containsList(getImagesSizeList(path), androidSlidesAndStarts);
				} else {
					return containsList(getImagesSizeList(path), iosSlidesAndStarts);
				}
			default:
				return isSuccess;

		}
		return isSuccess;
	}

	private static List<ImageSize> getImagesSizeList(String path) {
		List<ImageSize> imageSizeList = new ArrayList<ImageSize>();
		File f = new File(path);
		if (!f.exists() || !f.isDirectory()) {
			return null;
		}
		String[] filePaths = f.list();
		for (String filePath : filePaths) {
			String fPath = path + PackApk.PATH_SEPARATE + filePath;
			if (fPath.toLowerCase().endsWith(".png")) {
				ImageSize imageSize = new ImageSize(fPath);
				imageSizeList.add(imageSize);
			}
		}
		return imageSizeList;
	}

	// src是否包含所有dest中元素
	private static boolean containsList(List<ImageSize> src, List<ImageSize> dest) {
		boolean isContains = true;
		for (ImageSize size : dest) {
			if (!contains(src, size)) {
				writeToResult("错误:  \t缺少分辨率 " + size.width + "*" + size.height + "的PNG图片");
				isContains = false;
			}
		}
		return isContains;
	}

	private static boolean contains(List<ImageSize> imageSizes, ImageSize imageSize) {
		boolean isContains = false;
		for (ImageSize size : imageSizes) {
			if (size.width == imageSize.width && size.height == imageSize.height) {
				isContains = true;
				break;
			}
		}
		return isContains;
	}

	private static void writeToResult(String result) {
		System.out.println(result);
	}

	private static List<ImageSize> getDefaultSize(int[][] icons) {
		List<ImageSize> imageSizes = new ArrayList<ImageSize>();
		for (int i = 0; i < icons.length; i++)
			imageSizes.add(new ImageSize(icons[i][0], icons[i][1]));
		return imageSizes;
	}

	private static boolean checkAndroidConfig(String basePath) {

		boolean isResult = true;

		String name = getName(basePath);

		writeToResult("\n正在检测" + name + "的config文件....");
		File f = new File(basePath + PackApk.PATH_SEPARATE + CONFIG);
		if (!f.exists()) {
			System.out.println("错误:config文件不存在!");
			return false;
		}

		Properties properties = new Properties();
		FileInputStream in = null;
		try {
			in = new FileInputStream(basePath + PackApk.PATH_SEPARATE + CONFIG);
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		String applicationId = properties.getProperty("applicationId");
		if (applicationId == null || applicationId.trim().equals("")) {
			writeToResult("错误:" + name + "缺少applicationId");
			isResult = false;
		}

		String versionName = properties.getProperty("versionName");
		if (versionName == null || versionName.trim().equals("")) {
			writeToResult("错误:" + name + "缺少versionName");
			isResult = false;
		}

		String appName = properties.getProperty("appName");
		if (appName == null || appName.trim().equals("")) {
			writeToResult("错误:" + name + "缺少appName");
			isResult = false;
		}

		String tencentID = properties.getProperty("tencentID");
		if (tencentID == null || tencentID.trim().equals("")) {
			writeToResult("警告:" + name + "缺少tencentID");
		}

		String jpushAppKey = properties.getProperty("jpushAppKey");
		if (jpushAppKey == null || jpushAppKey.trim().equals("")) {
			writeToResult("警告:" + name + "缺少jpushAppKey");
		}

		String CYAN_APP_ID = properties.getProperty("CYAN_APP_ID");
		if (CYAN_APP_ID == null || CYAN_APP_ID.trim().equals("")) {
			writeToResult("警告:" + name + "缺少CYAN_APP_ID");
		}

		String CYAN_APP_KEY = properties.getProperty("CYAN_APP_KEY");
		if (CYAN_APP_KEY == null || CYAN_APP_KEY.trim().equals("")) {
			writeToResult("警告:" + name + "缺少CYAN_APP_KEY");
		}

		String DEPARTMENT_URL = properties.getProperty("DEPARTMENT_URL");
		if (DEPARTMENT_URL == null || DEPARTMENT_URL.trim().equals("")) {
			writeToResult("警告:" + name + "缺少DEPARTMENT_URL");
		}

		String URL = properties.getProperty("URL");
		if (URL == null || URL.trim().equals("") || !URL.trim().endsWith("/")) {
			writeToResult("错误:" + name + "缺少URL,或者URL不是以/结尾");
			isResult = false;
		}

		String APP_SECRET_SIGN = properties.getProperty("APP_SECRET_SIGN");
		if (APP_SECRET_SIGN == null || APP_SECRET_SIGN.trim().equals("")) {
			writeToResult("错误:" + name + "缺少APP_SECRET_SIGN");
			isResult = false;
		}

		String SITE_ID = properties.getProperty("SITE_ID");
		if (SITE_ID == null || SITE_ID.trim().equals("")) {
			writeToResult("错误:" + name + "缺少SITE_ID");
			isResult = false;
		}

		String CDN = properties.getProperty("CDN");
		if (CDN == null || CDN.trim().equals("") || (!CDN.trim().equals("true") && !CDN.trim().equals("false"))) {
			writeToResult("错误:" + name + "缺少CDN,或者CDN值有误");
			isResult = false;
		}

		writeToResult("\n" + name + "的config文件检测完毕!");

		return isResult;
	}

}
